package client.comm.baoding.ui

import android.content.Intent
import android.os.Bundle
import android.util.Log
import client.comm.commlib.toast.toast
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.page.OrderFragmentPageAdapter
import client.comm.baoding.alipay.AlipayUtils
import client.comm.baoding.api.bean.SuccessEvent
import client.comm.baoding.databinding.ActivityMeorderBinding
import client.comm.baoding.fragment.GoodsOrderTwoFragment
import client.comm.baoding.ui.vm.MeOrderViewModel
import com.blankj.utilcode.util.LogUtils
import com.pay.paytypelibrary.base.OnPayResultListener
import com.pay.paytypelibrary.base.OrderInfo
import com.pay.paytypelibrary.base.PayUtil
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class GoodsOrderActivity : BaseMiddleActivity<MeOrderViewModel, ActivityMeorderBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_meorder
    }

    val adapter: OrderFragmentPageAdapter by lazy {
        OrderFragmentPageAdapter(supportFragmentManager)
    }

    override fun initViewModel(): MeOrderViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.viewPager.adapter = adapter
        binding.tabLayout.setupWithViewPager(binding.viewPager)

        when (intent.getIntExtra("index", 0)) {
            0 -> {
//                binding.tabWrap.getChildAt(0).isSelected = true
            }

            1 -> {
                binding.viewPager.postDelayed({
                    binding.viewPager.setCurrentItem(1, false)
                }, 10)
            }

            2 -> {
                binding.viewPager.postDelayed({
                    binding.viewPager.setCurrentItem(2, false)
                }, 10)
            }

            3 -> {
                binding.viewPager.postDelayed({
                    binding.viewPager.setCurrentItem(3, false)
                }, 10)
            }

            4 -> {
                binding.viewPager.postDelayed({
                    binding.viewPager.setCurrentItem(4, false)
                }, 10)
            }
        }

        intent.getStringExtra("msg")?.let {
            toast(it)
        }
//        binding.viewPager.clearOnPageChangeListeners()
//        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
//            override fun onPageScrolled(
//                position: Int,
//                positionOffset: Float,
//                positionOffsetPixels: Int
//            ) {
//            }
//
//            override fun onPageSelected(position: Int) {
//                for ((i, c) in binding.tabWrap.children.withIndex()) {
//                    if (i != position) {
//                        c.isSelected = false
//                    } else {
//                        c.isSelected = true
//                    }
//                }
//            }
//
//            override fun onPageScrollStateChanged(state: Int) {
//
//            }
//
//        })

    }

    var tradeNo: String = ""
    override fun observerUI() {
        viewModel.orderPayCall.observe(
            this, {
                it?.let {
                    if (it.isSuccess) {
                        tradeNo = it.data!!.trade_no
//                        AlipayUtils(this).pay(it.data!!.pay_url)

                        when (it.data!!.pay_type) {

                            "3", "5", "7" -> {
                                val intent =
                                    Intent(this@GoodsOrderActivity, WebActivity::class.java)
                                        .apply {
                                            putExtra("adInfo", it.data!!.pay_url)
                                            putExtra("title", "支付")
                                        }
                                startActivity(intent)
                            }

                            else -> {
//                                PayUtil.CashierPaySingle(
//                                    this,
//                                    it.data!!.pay_data,
//                                    object : OnPayResultListener {
//                                        override fun onSuccess(orderInfo: OrderInfo) {
//                                        }
//
//                                        override fun onError(retMsg: String) {
//                                            toast(retMsg)
//                                        }
//                                    })
                            }
                        }
                    }
                }
            }
        )


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun zhihuanRefresh(event: String) {
        if (event.equals("zhihuan_success")) {
            LogUtils.i("zhihuan_success==")
            viewModel.pageZero.value = 0
            viewModel.holdZero()
//            viewModel.pageOne.value = 0
//            viewModel.holdOne()
            viewModel.pageTwo.value = 0
            viewModel.holdTwo()

            //GoodsOrderTwoFragment().adapter.notifyDataSetChanged()


//            adapter.notifyDataSetChanged()

//            viewModel.pageThree.value = 0
//            viewModel.holdThree()
        }
    }

    //支付成功的回调
    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun paySuccess(event: SuccessEvent?) {
        viewModel.check_order_pay(tradeNo)

    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        intent?.let {

            when (intent.getIntExtra("index", 0)) {
                0 -> {
//                binding.tabWrap.getChildAt(0).isSelected = true
                }

                1 -> {
                    binding.viewPager.postDelayed({
                        binding.viewPager.setCurrentItem(1, false)
                    }, 10)
                }

                2 -> {
                    binding.viewPager.postDelayed({
                        binding.viewPager.setCurrentItem(2, false)
                    }, 10)
                }

                3 -> {
                    binding.viewPager.postDelayed({
                        binding.viewPager.setCurrentItem(3, false)
                    }, 10)
                }

                4 -> {
                    binding.viewPager.postDelayed({
                        binding.viewPager.setCurrentItem(4, false)
                    }, 10)
                }

                else -> {}
            }

            intent.getStringExtra("msg")?.let {

                toast(it)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}